#include<iostream>
#include "TQueue.h"

template<class T> Nod<T>::Nod()
  {
    this->data = T();
    this->urm = 0;
  }//constructor

template<class T> Nod<T>::Nod(Nod& n)
  {
    this->data = n.data;
    this->urm = n.urm;
  }//constructor de copiere

template<class T> void Nod<T>::setData(T& d)
  {
    this->data = d;
  }//modificare data

template<class T> void Nod<T>::setUrm(Nod* n)
  {
    this->urm = n;
  }//modificare nod urmator

template<class T> void Nod<T>::setAnt(Nod* n)
  {
    this->ant = n;
  }//modificare nod anterior

template<class T> T Nod<T>::getData()
  {
    return this->data;
  }//return data

template<class T> Nod<T>* Nod<T>::getUrm()
  {
    return this->urm;
  }//return nod urmator

template<class T> Nod<T>* Nod<T>::getAnt()
  {
    return this->ant;
  }//return nod anterior

template<class T> TQueue<T>::TQueue()
  {
    this->b = this->e = 0;
    this->length = 0;
  }//constructor      

template<class T> void TQueue<T>::adauga(T d)
  {
    Nod<T>* nou = new Nod<T>();
    nou->setData(d);
    nou->setUrm(0);
    nou->setAnt(0);
    Nod<T>* q = this->e;
    if(q != 0)
      {
	q->setUrm(nou);
	nou->setAnt(q);
	this->e = this->e->getUrm();
      }
      else
        {
          this->b = this->e = nou;  
	}
    this->length++;
  }//adaugarea unui nod in lista

template<class T> void TQueue<T>::sterge()
  {
    Nod<T>* r = this->b;
    if(r == 0) std::cout<<"\n Lista este vida!";
      else
        {
          if(r->getUrm() == 0)
	    {
	      delete r;
	      this->b = this->e = 0;
	    }
	    else
	      {
		this->b = this->b->getUrm();
		this->b->setAnt(0);
		r->setUrm(0);
	        delete r;
	      }
	}
    this->length--;
  }//stergerea unui nod din lista

template<class T> void TQueue<T>::afisare()
  {
    Nod<T>* r;
    r = this->b;
    if(r == 0) std::cout<<"\nLista este vida!";
      else
        {
	  std::cout<<"\nLista:\n";
	  while(r != 0)
	    {
	      std::cout<<r->getData()<<"\n";
	      r = r->getUrm();
	    }
	}
    std::cout<<"\n";
  }//afisarea listei

template<class T> int TQueue<T>::nrElemente()
  {
    return this->length;
  }//returnarea nr de elemente

template<class T> void TQueue<T>::sortareC()
  {
    Nod<T> *r1, *r2;
    r1 = this->b;
    r2 = r1->getUrm();
    while(r1->getUrm() != 0)
      {
        r2 = r1->getUrm();
        while(r2 != 0)
          { 
	    if(r1->getData() > r2->getData())
	      {
	        T aux;
		aux = r1->getData();
		r1->setData(r2->getData());
		r2->setData(aux);
	      }
	      r2 = r2->getUrm();
	  }
	r1 = r1->getUrm();
      }
  }//sortare crescatoare a listei

template<class T> void TQueue<T>::sortareD()
  {
    Nod<T> *r1, *r2;
    r1 = this->b;
    r2 = r1->getUrm();
    while(r1->getUrm() != 0)
      {
        r2 = r1->getUrm();
        while(r2 != 0)
          {
	    if(r1->getData() < r2->getData())
	      {
	        T aux;
		aux = r1->getData();
		r1->setData(r2->getData());
		r2->setData(aux);
	      }
	      r2 = r2->getUrm();
	  }
	r1 = r1->getUrm();
      }
  }//sortare descrescatoare a listei
